import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// 1.2 引入别名包
import path from 'path'

import viteCompression from 'vite-plugin-compression'
import viteImagemin from 'vite-plugin-imagemin'

export default defineConfig({
  // 2. 插件相关
  plugins: [
    vue(),
    viteCompression({
      ext: ".gz",
      algorithm: "gzip",
      deleteOriginFile: false
    }),

    viteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false
      },
      optipng: {
        optimizationLevel: 7
      },
      mozjpeg: {
        quality: 20
      },
      pngquant: {
        quality: [0.8, 0.9],
        speed: 4
      },
      svgo: {
        plugins: [
          { name: "removeViewBox" },
          { name: "removeEmptyAttrs", active: false }
        ]
      }
    })
    
  ],

  // 2.1 允许使用less
  css: {
    preprocessorOptions: {
      less: {
        math: 'always',
        globalVars: {
          color: '#FB87B6'
        }
      }
    }
  },

  // 3. 配置别名
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },

  // 4. 虚拟服务配置
  server: {
    port: 8080,
    open: true,
    host: '0.0.0.0',
    
    // 4.1 网络代理
    proxy: {
      '/dev-api': {
        target: 'http://192.168.3.25:2001/',
        rewrite: path => path.replace(/^\/dev-api/, ''),
        changeOrigin: true
      },
      '/api': {
        target: 'http://192.168.3.25:9401/',
        rewrite: path => path.replace(/^\/api/, ''),
        changeOrigin: true
      }
    }
  }
})
